package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ahpu.propertymanage.dataclass.*;
import edu.ahpu.propertymanage.pojo.Dispatch;
import edu.ahpu.propertymanage.pojo.Project;
import edu.ahpu.propertymanage.pojo.Repair;
import edu.ahpu.propertymanage.pojo.Security;
import edu.ahpu.propertymanage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/worker")
public class WorkerProjectController {

    @Autowired
    WorkerService workerService;

    @Autowired
    ProjectService projectService;

    @Autowired
    DispatchService dispatchService;

    @Autowired
    SecurityService securityService;

    @Autowired
    RepairService repairService;


    //派工表
    @PostMapping("dispatchworker")
    public WorkerProjectPage<DisProject> dispatchWorker(@RequestBody PageSet pageSet) {

        WorkerProjectPage<DisProject> workerProjectPage = new WorkerProjectPage<>();
        //总页数
        pageSet.setTotal(projectService.countDis(pageSet));
        //分页数据
        List<DisProject> disProjects = projectService.disProject(pageSet);

//        设置msg是否填写
        for (DisProject project : disProjects) {
            if (project.getDispatchMsg() == null || "".equals(project.getDispatchMsg()))
                project.setBool(false);
            else
                project.setBool(true);
        }

        //返回数据对象
        workerProjectPage.setPageSet(pageSet);
        workerProjectPage.setList(disProjects);
        return workerProjectPage;
    }

    //保存派工表
    @PostMapping("updatedispatch")
    public String updateDispatch(@RequestBody DisProject disProject) {
        //创建一个派工对象
        QueryWrapper<Dispatch> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dispatch::getProjectId, disProject.getProjectId());
        Dispatch dispatch = dispatchService.getOne(wrapper);
        //将msg存入数据库
        dispatch.setDispatchMsg(disProject.getDispatchMsg());
        boolean bool = dispatchService.updateById(dispatch);
        if (bool)
            return "success";
        else
            return "error";
    }


    //安全检查表
    @PostMapping("securityproject")
    public WorkerProjectPage<SecProject> securityWorker(@RequestBody PageSet pageSet) {
        WorkerProjectPage<SecProject> workerProjectPage = new WorkerProjectPage<>();
        //总页数
        pageSet.setTotal(projectService.countSecurity(pageSet));
        //分页数据
        List<SecProject> secProjects = projectService.secProject(pageSet);

        //设置msg是否填写
        for (SecProject project : secProjects) {
            if (project.getSecurityMsg() == null || "".equals(project.getSecurityMsg()))
                project.setBool(false);
            else
                project.setBool(true);
        }

        //返回数据对象
        workerProjectPage.setPageSet(pageSet);
        workerProjectPage.setList(secProjects);
        return workerProjectPage;
    }


    //保存安全检查表
    @PostMapping("updatesecurity")
    public String updateSecurity(@RequestBody SecProject secProject) {
        //创建一个派工对象
        QueryWrapper<Security> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Security::getProjectId, secProject.getProjectId());
        Security security = securityService.getOne(wrapper);
        //将msg存入数据库
        security.setSecurityMsg(secProject.getSecurityMsg());
        boolean bool = securityService.updateById(security);
        if (bool)
            return "success";
        else
            return "error";
    }

    //维修表
    @PostMapping("repairproject")
    public WorkerProjectPage<RepProject> repairWorker(@RequestBody PageSet pageSet) {
        WorkerProjectPage<RepProject> workerProjectPage = new WorkerProjectPage<>();
        //总页数
        pageSet.setTotal(projectService.countRepair(pageSet));
        //分页数据
        List<RepProject> repProjects = projectService.repProject(pageSet);

        //设置msg是否填写
        for (RepProject project : repProjects) {
            if (project.getRepairMsg() == null || "".equals(project.getRepairMsg()))
                project.setBool(false);
            else
                project.setBool(true);
        }

        //返回数据对象
        workerProjectPage.setPageSet(pageSet);
        workerProjectPage.setList(repProjects);
        return workerProjectPage;
    }


    //保存维修表
    @PostMapping("updaterepair")
    public String updateRepair(@RequestBody RepProject repProject) {
        //创建一个派工对象
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Repair::getProjectId, repProject.getProjectId());
        Repair repair = repairService.getOne(wrapper);
        //将cost存入数据库
        Project project = new Project();
        project.setProjectId(repProject.getProjectId());
        project.setProjectCost(repProject.getProjectCost());
        boolean probool = projectService.updateById(project);
        //将msg存入数据库
        repair.setRepairMsg(repProject.getRepairMsg());
        boolean bool = repairService.updateById(repair);
        if (bool&&probool)
            return "success";
        else
            return "error";
    }
}
